﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.ComponentModel;


namespace baseClass.Controls
{
    public class gridProdStageDetail : common.controls.baseDataGridView 
    {
        public enum ColumnNames
        {
            itemCode, itemName, itemUnit, prodStage, unitCost
        };

        public gridProdStageDetail()
        {
            DataAccess.DataCache.LoadProductionStage(false);
            this.SetColorType(common.Theme.GridType.Edit);
            this.myOnNewLineAdd += new common.controls.baseDataGridView.OnNewLineAdd(OnAddNewLine);
            this.myOnFind += new OnCellEvent(OnFindData);
        }

        public void UpdateItemList()
        {
            data.baseDS.geProdStageDetailDataTable dataTbl = (data.baseDS.geProdStageDetailDataTable)myEditDataSource.DataSource;
            for (int idx = 0; idx < dataTbl.Count; idx++)
            {
                if (dataTbl[idx].RowState == DataRowState.Deleted) continue;
                DataAccess.DataCache.FindItem(dataTbl[idx].itemCode);
            }
        }
        public void UpdateItemList(string itemCode)
        {
            DataAccess.DataCache.FindItem(itemCode);
        }

        public override void Init()
        {
            base.Init();
            common.Theme.ColorSet csDisable = common.ThemeLibs.GetColorSetINFO();
            data.baseDS.geProdStageDetailDataTable dummyTbl = new data.baseDS.geProdStageDetailDataTable();

            // =============================
            // detailGrid
            // =============================

            // itemCode
            // 
            common.controls.DataGridViewTextBoxColumnExt itemCode = new common.controls.DataGridViewTextBoxColumnExt();
            itemCode.Uppercase = true;
            itemCode.DataPropertyName = dummyTbl.itemCodeColumn.ColumnName;
            itemCode.HeaderText = "Mã.hàng";
            itemCode.Name = ColumnNames.itemCode.ToString();
            itemCode.Width = 130;
            // 
            // itemName
            // 
            DataGridViewComboBoxColumn itemName = new DataGridViewComboBoxColumn();
            BindingSource itemSrc = new BindingSource();
            itemSrc.DataSource = DataAccess.DataCache.myBaseDS.geItem;

            itemName.DataSource = itemSrc;
            itemName.DataPropertyName = dummyTbl.itemCodeColumn.ColumnName;
            itemName.DisplayMember = DataAccess.DataCache.myBaseDS.geItem.nameColumn.ColumnName;
            itemName.ValueMember = DataAccess.DataCache.myBaseDS.geItem.codeColumn.ColumnName;
            itemName.HeaderText = "Tên.hàng";
            itemName.Name = ColumnNames.itemName.ToString();
            itemName.ReadOnly = true;
            
            itemName.Width = 250;
            itemName.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            itemName.DefaultCellStyle.BackColor = csDisable.BackColor;
            itemName.DefaultCellStyle.ForeColor = csDisable.ForeColor;

            // 
            // itemUnit
            // 
            DataGridViewComboBoxColumn itemUnit = new DataGridViewComboBoxColumn();
            itemUnit.DataSource = itemSrc;
            itemUnit.DataPropertyName = dummyTbl.itemCodeColumn.ColumnName;
            itemUnit.DisplayMember = DataAccess.DataCache.myBaseDS.geItem.unitColumn.ColumnName;
            itemUnit.ValueMember = DataAccess.DataCache.myBaseDS.geItem.codeColumn.ColumnName;
            itemUnit.HeaderText = "ĐVT";
            itemUnit.Name = ColumnNames.itemUnit.ToString();
            itemUnit.ReadOnly = true;

            itemUnit.Width = 50;
            itemUnit.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            itemUnit.DefaultCellStyle.BackColor = csDisable.BackColor;
            itemUnit.DefaultCellStyle.ForeColor = csDisable.ForeColor;
            itemUnit.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            // 
            // prodStage
            // 
            BindingSource productionStageSrc = new BindingSource();
            itemSrc.DataSource = DataAccess.DataCache.myBaseDS.productionStage;

            DataGridViewComboBoxColumn prodStage = new DataGridViewComboBoxColumn();
            prodStage.DataPropertyName = dummyTbl.prodStageColumn.ColumnName;
            prodStage.DataSource = productionStageSrc;
            prodStage.DisplayMember = DataAccess.DataCache.myBaseDS.productionStage.descriptionColumn.ColumnName;
            prodStage.ValueMember = DataAccess.DataCache.myBaseDS.productionStage.codeColumn.ColumnName;
            prodStage.HeaderText = "Công đọan";
            prodStage.Name = ColumnNames.prodStage.ToString();
            prodStage.DisplayStyle = DataGridViewComboBoxDisplayStyle.DropDownButton;
            prodStage.Width = 200;

            // 
            // unitCost
            // 
            common.controls.DataGridViewTextBoxColumnExt unitCost = new common.controls.DataGridViewTextBoxColumnExt();
            unitCost.DataPropertyName = dummyTbl.unitCostColumn.ColumnName;
            unitCost.DefaultCellStyle = application.SysLibs.CellTypePRICE();
            unitCost.HeaderText = "Đơn giá";
            unitCost.Name = ColumnNames.unitCost.ToString();
            unitCost.Width = 100;

            this.Columns.Clear();
            this.Columns.AddRange(new DataGridViewColumn[] {
                    itemCode,itemName,itemUnit,prodStage,unitCost
            });


            this.myAutoFitColumn = ColumnNames.itemName.ToString();
            this.AutoFit();

            this.DisableReadOnlyColumns();
        }        

        protected override void OnCellValueChanged(DataGridViewCellEventArgs e)
        {
            base.OnCellValueChanged(e);
            if (this.CurrentCell == null || !this.IsValidCell(e.RowIndex, e.ColumnIndex)) return;
            try
            {
                if (this.IsOnProcessing) return;
                this.IsOnProcessing = true;

                if (this.Columns[e.ColumnIndex].Name == ColumnNames.itemCode.ToString())
                {
                    UpdateItemList(this.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
                }
                this.IsOnProcessing = false;
            }
            catch (Exception er)
            {
                this.IsOnProcessing = false;
                this.ErrorHandler(this,er);
            }
        }
        public override void Refresh()
        {
            if (IsOnProcessing) return;
            try
            {
                IsOnProcessing = true;
                UpdateItemList();
                IsOnProcessing = false;
                base.Refresh();
            }
            catch (Exception er)
            {
                IsOnProcessing = false;
                ErrorHandler(this, er);
            }
        }

        public override bool CheckData()
        {
            this.myEditDataSource.EndEdit();
            return DoValidateData(OnRowValidate);
        }

        private void OnRowValidate(DataGridViewRow row, common.controls.GridRowValidateEventArgs e)
        {
            if (row.DataBoundItem == null) return;
            if (this.Columns[ColumnNames.itemCode.ToString()].Visible && row.Cells[ColumnNames.itemCode.ToString()].Value.ToString().Trim() == "")
            {
                e.ErrorText.Add(String.Format(CommonTypes.language.GetErrorText("DataEmpty1"), this.Columns[ColumnNames.itemCode.ToString()].HeaderText));
                e.HaveError = true;
            }

            if (this.Columns[ColumnNames.prodStage.ToString()].Visible && row.Cells[ColumnNames.prodStage.ToString()].Value.ToString().Trim() == "")
            {
                e.ErrorText.Add(String.Format(CommonTypes.language.GetErrorText("DataEmpty1"), this.Columns[ColumnNames.prodStage.ToString()].HeaderText));
                e.HaveError = true;
            }
            //=========================
            //Check data contrainsts
            //=========================
            if (this.Columns[ColumnNames.itemCode.ToString()].Visible &&
                row.Cells[ColumnNames.itemCode.ToString()].Value.ToString().Trim() != "" &&
                DataAccess.DataCache.FindItem(row.Cells[ColumnNames.itemCode.ToString()].Value.ToString()) == null)
            {
                e.ErrorText.Add(String.Format(CommonTypes.language.GetErrorText("DataInvalid1"), this.Columns[ColumnNames.itemCode.ToString()].HeaderText));
                e.HaveError = true;
            }
        }

        private void OnAddNewLine(object sender, DataGridViewRow currentRow, DataGridViewRowEventArgs addedRow)
        {
            if (addedRow == null) return;

            string saveProdStage = null;

            if (currentRow != null)
            {
                saveProdStage = currentRow.Cells[ColumnNames.prodStage.ToString()].EditedFormattedValue.ToString();
            }
            if (this.myEditDataSource.Current != null)
                DataAccess.DataLibs.InitData((data.baseDS.geProdStageDetailRow)((DataRowView)this.myEditDataSource.Current).Row);
            if (this.Columns[ColumnNames.prodStage.ToString()].Visible) addedRow.Row.Cells[ColumnNames.prodStage.ToString()].Value = saveProdStage;
        }
        private void OnFindData(object sender, common.controls.GridEventArgs e)
        {
            string findCode = this.Rows[e.RowIndex].Cells[e.ColumnIndex].EditedFormattedValue.ToString(); ;
            if (this.CurrentCell == null) return;
            //Code
            if (this.Columns[e.ColumnIndex].Name.Trim() == ColumnNames.itemCode.ToString())
            {
                baseClass.Libs.myItemFind.Find(findCode, true);
                data.baseDS.geItemRow itemRow = baseClass.Libs.myItemFind.mySelectedDataRow;
                if (itemRow != null)
                    this.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = itemRow.code;
                else this.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = DBNull.Value;
                this.myEditDataSource.ResetBindings(false);
            }
        }
    }
}
